<?php
	require_once('/IUserRepository.php');
	require_once('/../Models/User.php');
	require_once('/../Models/Role.php');

	class UserRepository implements IUserRepository{
		private $db;

		function __construct() {
			$this->db = new Database();
		}

		public function getUserByID($userID) {
			$sql = "SELECT * FROM $this->db->usersTable WHERE ID = $userID";
			$users = $this->db->query($sql);
			$user = $users[0];
			return new User($user["ID"], $user["Name"], $user["Email"], $user["Password"]);
		}

		public function getUsersByProject($projectID) {
			$sql = "SELECT " .  $this->db->usersTable . ".* ";
			$sql .= "FROM " . $this->db->usersTable . " , " . $this->db->userRolesTable;
			$sql .= " WHERE " . $this->db->userRolesTable . ".ProjectID = ". $projectID;
			$sql .= " AND " . $this->db->usersTable . ".ID = " . $this->db->userRolesTable . ".UserID";

			$users = $this->db->query($sql);
			
			$returnUsers = array();
			
			for($i = 0; $i < sizeof($users); $i++) {
				$user = $users[$i];
				$returnUser = new User($user["ID"], $user["Name"], $user["Email"], $user["Password"]);
				array_push($returnUsers, $returnUser);
			}

			return $returnUsers;
		}

		public function getUserRole($user, $projectID){
			$sql = "SELECT " . $this->db->rolesTable . ".* ";
			$sql .= "FROM " . $this->db->userRolesTable . ", " . $this->db->rolesTable;
			$sql .= " WHERE " . $this->db->userRolesTable . ".UserID = ". $user->ID;
			$sql .= " AND " . $this->db->userRolesTable . ".ProjectID = ". $projectID;
			$sql .= " AND " . $this->db->rolesTable . ".ID = " . $this->db->userRolesTable . ".RoleID";
			$roles = $this->db->query($sql);
			$role = $roles[0];
			return new Role($role["ID"], $role["Title"]);

		}
	}
		
?>